package com.baidu.day05;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        GoodsService goodsService = new GoodsService();
        UserService userService = new UserService();
        System.out.println("===============欢迎使用超市管理系统===============");

        System.out.println("请选择操作: 5、注册  6、登录  7、查看用户信息  4、退出系统");
        while (true) {
            Scanner myScanner = new Scanner(System.in);
            String userInput = myScanner.next();
            switch (userInput) {
                case UserConst.REGISTER:
                    userRegister(myScanner, userService);
                    break;

                case UserConst.LOGIN:
                    userLogin(myScanner, userService, goodsService);
                    break;

                case UserConst.FIND_ALL:
                    userService.findAll();
                    break;

                case SystemConst.EXIST:
                    System.out.println("感谢您的使用");
                    System.exit(0);//退出jvm
            }
        }
    }
    //注册的方法
    private static void userRegister(Scanner myScanner, UserService userService) {
        System.out.println("请输入用户的账号");
        String account = myScanner.next();
        System.out.println("请输入用户的密码");
        String password = myScanner.next();
        System.out.println("请确认密码");
        String confirmPassword = myScanner.next();
        if (!password.equals(confirmPassword)) {
            System.out.println("两次输入的密码不一致，请重新输入");
            return;
        }
        int id = UserService.userList.size() + 1;
        User user = new User(id, account, password, 0.0);
        boolean flag = userService.register(user);
        System.out.println(flag ? "注册成功" : "注册失败");
    }

    //登录的方法
    private static void userLogin(Scanner myScanner, UserService userService, GoodsService goodsService) {
        System.out.println("请输入用户的账号");
        String loginAccount = myScanner.next();
        System.out.println("请输入用户的密码");
        String loginPassword = myScanner.next();
        User loginUser = userService.login(loginAccount, loginPassword);
        if (loginUser == null) {
            System.out.println("用户名或密码错误，请重新输入");
        } else {
            if (loginUser.getAccount().equals("admin")) {
                //显示管理员菜单
                showAdminMenu(goodsService);
            } else {
                //显示普通用户菜单
                showUserMenu(myScanner,goodsService,loginUser,userService);
            }
        }
    }

    //管理员菜单的方法
    private static void showAdminMenu (GoodsService goodsService){
        while (true) {
            System.out.println("请选择操作1、查看所有商品  2、添加商品  3、删除商品  4、退出系统");
            Scanner myScanner = new Scanner(System.in);
            String userInput = myScanner.next();
            switch (userInput) {
                case GoodsConst.FILE_ALL:
                    goodsService.findAll();
                    break;
                case GoodsConst.ADD:
                    System.out.println("请输入要添加的商品编号");
                    int goodsNum = myScanner.nextInt();
                    System.out.println("请输入要添加的商品名称");
                    String goodsName = myScanner.next();
                    System.out.println("请输入要添加的商品价格");
                    double goodsPrice = myScanner.nextDouble();
                    System.out.println("请输入要添加的商品库存");
                    int goodsStock = myScanner.nextInt();

                    Goods goods = new Goods(goodsNum, goodsName, goodsPrice, goodsStock);
                    boolean flag = goodsService.add(goods);
                    System.out.println(flag ? "添加成功" : "添加失败");
                    break;
                case GoodsConst.REMOVE:
                    System.out.println("请输入要删除的商品名称");
                    String name = myScanner.next();
                    goodsService.remove(name);
                    break;
                case SystemConst.EXIST:
                    System.out.println("感谢您的使用");
                    System.exit(0);//
            }
        }
    }

    //普通用户菜单的方法
    private static void showUserMenu(Scanner myScanner,GoodsService goodsService,
                                     User user, UserService userService) {
        System.out.println("欢迎" + user.getAccount() + "回来");
        while(true) {
            System.out.println("请选择操作: 1、查看所有商品  8、购买商品  4、退出系统");
            String userInput = myScanner.next();
            switch (userInput) {
                case GoodsConst.FILE_ALL:
                    goodsService.findAll();
                    break;
                case UserConst.BUY:
                    System.out.println("请输入要购买的商品名称");
                    String goodsName = myScanner.next();
                    System.out.println("请输入要购买的商品数量");
                    int goodsNum = myScanner.nextInt();
                    userService.buy(goodsName,goodsNum);
                    break;
                case SystemConst.EXIST:
                    System.out.println("感谢您的使用");
                    System.exit(0);
            }
        }
    }

}
